package com.jp.knowledge.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by jiayen on 16/11/1.
 */

public class TimeUtil {

    /**
     * 将时间戳转为代表"距现在多久之前"的字符串
     * @param timeStr   时间戳
     * @return
     */
    public static String getStandardDate(String timeStr) {

        ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<>();


        long t = Long.parseLong(timeStr);
        return getStandard(t);
    }

    public static String getStandard(long t) {

        StringBuffer sb = new StringBuffer();
        long time =t/1000;// System.currentTimeMillis() - (t*1000);
        long mill = time;//秒前

        long minute = time/(60*10000);// 分钟前

        long hour = time/(60*60*10000);// 小时

        long day = time/(24*60*60*10000);// 天前
        L.e("TimeUtil==="+t+"=====day="+day+"==========hour="+hour+"========minute="+minute);
        if (day - 1 > 0) {
            sb.append(day + "天");
        } else if (hour - 1 > 0) {
            if (hour >= 24) {
                sb.append("1天");
            } else {
                sb.append(hour + "小时");
            }
        } else if (minute - 1 > 0) {
            if (minute == 60) {
                sb.append("1小时");
            } else {
                sb.append(minute + "分钟");
            }
        } else if (mill - 1 > 0) {
            if (mill == 60) {
                sb.append("1分钟");
            } else {
                sb.append(mill + "秒");
            }
        } else {
            sb.append("刚刚");
        }
        if (!sb.toString().equals("刚刚")) {
            sb.append("前");
        }
        return sb.toString();
    }

    public final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd hh:MM:ss");
        }
    };



    public final static ThreadLocal<SimpleDateFormat> dateFormaterChina = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy年MM月dd日 hh:MM:ss");
        }
    };

    public final static ThreadLocal<SimpleDateFormat> date_year_month = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd");
        }
    };
    public final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
//            return new SimpleDateFormat("yyyy-MM-dd hh:mm");
            return new SimpleDateFormat("yyyy年MM月dd日");
        }
    };
    public final static ThreadLocal<SimpleDateFormat> refreshTime = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("H:mm ");
        }
    };

    public static String formatYearData(long time){
        return date_year_month.get().format(new Date(time));
    }
    /**
     * 格式化中文格式
     * @param time
     * @return
     */
    public static String formatData(long time){
        return  dateFormaterChina.get().format(new Date(time));
    }
    /**
     * 以友好的方式显示时间
     * @param sdate
     * @return
     */
    public static String friendly_time(long sdate) {
        if(sdate <= 0)
            sdate = System.currentTimeMillis();
        Date time =new Date(sdate);
        if(time == null) {
            return "Unknown";
        }
        String ftime = "";
        Calendar cal = Calendar.getInstance();

        //判断是否是同一天
        String curDate = dateFormater.get().format(cal.getTime());
        String paramDate = dateFormater.get().format(time);
        if(curDate.equals(paramDate)){
            int hour = (int)((cal.getTimeInMillis() - time.getTime())/3600000);
            if(hour == 0) {
                ftime = Math.max((cal.getTimeInMillis() - time.getTime()) / 60000, 1) + "分钟前";
            }else if (hour < 0) {
                ftime = "刚刚";
            }else {
                ftime = hour + "小时前";
            }
            return ftime;
        }

        long lt = time.getTime()/86400000;
        long ct = cal.getTimeInMillis()/86400000;
        int days = (int)(ct - lt);
        if(days == 0){
            int hour = (int)((cal.getTimeInMillis() - time.getTime())/3600000);
            if(hour == 0) {
//                ftime = Math.max((cal.getTimeInMillis() - time.getTime()) / 60000, 1) + "分钟前";
                int minute = (int) ((cal.getTimeInMillis() - time.getTime()) / 60000);
                ftime = (minute < 1) ? "刚刚" : minute + "分钟前";
            }else if (hour < 0) {
                ftime = "刚刚";
            }else {
                ftime = hour + "小时前";
            }
        }
        else if(days == 1){
            ftime = "昨天";
        }
        else if(days == 2){
            ftime = "前天";
        }
        else if(days > 2 && days <= 30){
            ftime = days+"天前";
        }
        else if(days > 30&&days<365){
            int month = days/30;
            ftime =month+"月前";
        }else if(days>365&&days<365*10){
            int year = days/365;
            ftime =year+"年前";
        }else{
            ftime=dateFormater2.get().format(time);
        }
        return ftime;
    }

    public static String getRefreshTime(long stime){
        Date time =new Date(stime);

        if(time == null) {
            return "Unknown";
        }
        String ftime = "";
        Calendar cal = Calendar.getInstance();

        //判断是否是同一天
        String curDate = dateFormater.get().format(cal.getTime());
        String paramDate = dateFormater.get().format(time);
        if(curDate.equals(paramDate)){//是今天
            ftime = "今天"+ refreshTime.get().format(time);
            return ftime;
        }


        long lt = time.getTime()/86400000;
        long ct = cal.getTimeInMillis()/86400000;
        int days = (int)(ct - lt);
        if(days == 0){
            ftime = "今天"+ refreshTime.get().format(time);
        }
        else if(days == 1){
            ftime = "昨天"+ refreshTime.get().format(time);
        }
        else if(days == 2){
            ftime = "前天"+ refreshTime.get().format(time);
        }else{
            ftime = dateFormater.get().format(time);
        }

        return  ftime;
    }
}
